<?php
/* --------------------------------------------------------------
   PropertiesCombisAdminView.php 2009-12-14 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2009 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/


class PropertiesCombisAdminView 
{
	var $v_coo_combis_admin_control = false;
	
	var $m_env_get_array = array();
	var $m_env_post_array = array();
	
	var $m_content_array = array();
	var $m_content_template = '';
	
	function PropertiesCombisAdminView($p_get_array, $p_post_array) 
	{
		$this->v_coo_combis_admin_control = new PropertiesCombisAdminControl();
		$this->v_coo_properties_admin_control = new PropertiesAdminControl();
		
		$this->m_env_get_array 	= $p_get_array;
		$this->m_env_post_array = $p_post_array;
	}
	
	function proceed()
	{
		$t_output = '';
		if(isset($this->m_env_get_array['action']) == false) trigger_error('missing action', E_USER_ERROR);
		
		switch($this->m_env_get_array['action'])
		{
			case 'save':
				$t_products_properties_combis_id = $this->v_coo_combis_admin_control->save_properties_combis($this->m_env_post_array);				
				$t_output = (int)$t_products_properties_combis_id;
				
				$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
				$coo_properties_data_agent->rebuild_properties_index($this->m_env_post_array['products_id']);
				break;

			case 'delete':
				if(isset($this->m_env_post_array['properties_combis_id_array'])) {
					$c_properties_combis_id_array = $this->m_env_post_array['properties_combis_id_array'];
				} else {
					 trigger_error('missing properties_combis_id', E_USER_ERROR);
				}
				$this->v_coo_combis_admin_control->delete_properties_combis($c_properties_combis_id_array);
				$t_output = '';
				break;
				
			case 'load':
				switch($this->m_env_get_array['type'])
				{
					case 'properties_combis_table':
						if(isset($this->m_env_get_array['properties_combis_id'])) {
							$c_properties_combis_id = (int)$this->m_env_get_array['properties_combis_id'];
						} else {
							 trigger_error('missing properties_combis_id', E_USER_ERROR);
						}
						$t_output = $this->get_properties_combis_table($c_properties_combis_id, $_SESSION['languages_id']);
						break;
						
					default:
						trigger_error('unknown type: '. $this->m_env_get_array['type'], E_USER_ERROR);
				}
				
				break;

			default:
				trigger_error('unknown action: '. $this->m_env_get_array['action'], E_USER_ERROR);
		}
		return $t_output;
	}
	
	
	function get_properties_combis_main($p_products_id)
	{
		$c_products_id = (int)$p_products_id;
		$p_language_id = $_SESSION['languages_id'];
		
		$t_properties_id_array = $this->v_coo_properties_admin_control->get_all_properties_ids();
		$t_properties_data_array = array();
		
		foreach($t_properties_id_array as $t_properties_id)
		{
			$t_properties_data_array[] = $this->v_coo_properties_admin_control->get_properties_full_struct($t_properties_id, $p_language_id);
		}
		
		$t_combis_id_array = $this->v_coo_combis_admin_control->get_all_properties_combis_ids($c_products_id);
		$t_combis_tables_array = array();
		
		foreach($t_combis_id_array as $t_combis_id)
		{
			$t_combis_tables_array[] = array(
											'properties_combis_id' => $t_combis_id,
											'table_html'		   => $this->get_properties_combis_table($t_combis_id, $p_language_id)
										);
		}		
		
		if(count($t_combis_tables_array) > 0){
			$available_properties = $this->v_coo_combis_admin_control->get_admin_select($c_products_id);
			//print_r($available_properties);
			foreach($t_properties_data_array AS $key => $value){
				if(!array_key_exists($value['properties_id'], $available_properties)){
					$t_properties_data_array[$key]['class'] = " disable";
				}else{
					$t_properties_data_array[$key]['class'] = "";					
				}
			}
		}
		
		$gm_array = array();
		$t_sql = '	SELECT
						products_name,
						properties_dropdown_mode,
						use_properties_combis_weight,
						use_properties_combis_quantity,
						use_properties_combis_shipping_time,
                                                properties_show_price
					FROM
						products AS p
					LEFT JOIN products_description AS pd USING(products_id)
					WHERE
						p.products_id = "'.$c_products_id.'"
						AND language_id = "'. $p_language_id.'"';
		
		$gm_query = xtc_db_query($t_sql);
		$gm_array = xtc_db_fetch_array($gm_query);
		$t_products_name = $gm_array['products_name'];
		$t_properties_dropdown_mode = $gm_array['properties_dropdown_mode'];
		$t_use_properties_combis_weight = $gm_array['use_properties_combis_weight'];
		$t_use_properties_combis_quantity = $gm_array['use_properties_combis_quantity'];
		$t_use_properties_combis_shipping_time = $gm_array['use_properties_combis_shipping_time'];
                $t_properties_show_price = $gm_array['properties_show_price'];
		
		$t_content_data_array = array(
									'PROPERTIES_TABLES'			=> $t_properties_data_array,
									'PROPERTIES_COMBIS_TABLES' 	=> $t_combis_tables_array,
									'products_id'				=> $c_products_id,
									'products_name'				=> $t_products_name,
									'products_path'				=> xtc_href_link(FILENAME_CATEGORIES, "cPath=".$this->m_env_get_array['cPath']."&pID=".$c_products_id."&action=new_product"),
									'properties_dropdown_mode'	=> $t_properties_dropdown_mode,
                                                                        'use_properties_combis_weight'	=> $t_use_properties_combis_weight,
                                                                        'use_properties_combis_quantity'	=> $t_use_properties_combis_quantity,
                                                                        'use_properties_combis_shipping_time'	=> $t_use_properties_combis_shipping_time,
                                                                        'properties_show_price'	=> $t_properties_show_price
								);
		$t_content_template = 'combis_main.html';
		
		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
		return $t_html_output;
	}
	
	function get_properties_combis_table($p_properties_combis_id, $p_language_id)
	{
		$c_properties_combis_id = (int)$p_properties_combis_id;
		$c_language_id = (int)$p_language_id;
		
		#get data array for assigning in smarty template
		$t_content_data_array = $this->v_coo_combis_admin_control->get_combis_full_struct($c_properties_combis_id, $c_language_id);
		$t_content_template = 'combis_table.html';
		
		// get products quantity type
		$coo_quantity_unit = MainFactory::create_object('QuantityUnit');
		$t_quantity_unit_name = $coo_quantity_unit->get_quantity_unit_name_by_products_id($t_content_data_array['products_id'], $c_language_id);
		
		$t_content_data_array['products_quantity_unit_name'] = $t_quantity_unit_name;
		
		$t_html_output = $this->build_html($t_content_data_array, $t_content_template);
		return $t_html_output;
	}
	
	function build_html($p_content_data_array, $p_template_file, $p_add_languages=true)
	{
		#language array for assigning in smarty template
		if($p_add_languages)
		{
			$t_languages_array = xtc_get_languages();
			$p_content_data_array = array_merge(
										array('LANGUAGES' => $t_languages_array), 
										$p_content_data_array);
		}
		$coo_smarty = new Smarty();
		
		# assign content
		$coo_smarty->assign('content_data', $p_content_data_array);
		
		# compile settings
		$coo_smarty->caching = false;
		$coo_smarty->compile_dir = DIR_FS_CATALOG.'templates_c/';
		
		# get html content
		$t_html_output = $coo_smarty->fetch(DIR_FS_CATALOG.'admin/templates/properties/'.$p_template_file);
		return $t_html_output;
	}
	
}

?>